package compiler;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import javax.swing.*;
import javax.swing.border.*;
import basic.MySqlDB;

/**
 * The Class SettingDatabaseGui.
 */
public class SettingDatabaseGui extends JFrame implements ActionListener {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The default close operation. */
	private int defaultCloseOperation = JFrame.DISPOSE_ON_CLOSE;

	/** The instance. */
	private static SettingDatabaseGui instance = null;

	/** The j label_ i l6. */
	private JLabel jLabel_IL1, jLabel_IL4, jLabel_IL5, jLabel_IL8, jLabel_IL6;

	/** The j text_ i l5. */
	private JTextField jText_IL1, jText_IL4, jText_IL5;

	/** The j text_ i l6. */
	private JPasswordField jText_IL6;

	/** The j label_ i l7. */
	private JButton jLabel_IL7;

	/** The defaults button. */
	private JButton saveButton, defaultsButton;;

	/** The app panel. */
	private JPanel appPanel;

	/** The db. */
	private MySqlDB db;

	/** The config file. */
	private File configFile;

	/** The user props. */
	private Properties sysProps, userProps;

	/**
	 * Instantiates a new setting database gui.
	 */
	private SettingDatabaseGui() {
		init();
	}

	/**
	 * Gets the single instance of SettingDatabaseGui.
	 * 
	 * @return single instance of SettingDatabaseGui
	 */
	public static SettingDatabaseGui getInstance() {
		if (instance == null) {
			instance = new SettingDatabaseGui();
		}
		return instance;
	}

	/**
	 * Already instanced.
	 * 
	 * @return true, if successful
	 */
	public static boolean alreadyInstanced() {
		if (instance == null)
			return false;
		else
			return true;
	}

	/**
	 * screens the selections of the property's application
	 */
	protected void init() {
		this.setDefaultCloseOperation(defaultCloseOperation);
		this.setSize(350, 700);
		this.setLayout(new BorderLayout());
		this.setResizable(false);
		this.setTitle("Configurazione Connessione database");
		configFile = new File("config.xml");
		JPanel optionsPanel = new JPanel();
		appPanel = new JPanel();
		String titolo = "Impostazioni";
		appPanel.setBorder(new TitledBorder(new EtchedBorder(), titolo));

		jLabel_IL1 = new JLabel();
		jLabel_IL1.setText("Selezionare il nome del server");
		jLabel_IL1.setBounds(18, 38, 310, 25);
		jText_IL1 = new JTextField();
		jText_IL1.setText((""));
		appPanel.add(jLabel_IL1);
		appPanel.add(jText_IL1);
		jText_IL1.setBounds(18, 68, 310, 25);

		jLabel_IL4 = new JLabel();
		jLabel_IL4.setText("Selezionare il nome del database");
		jLabel_IL4.setBounds(18, 118, 310, 25);
		jText_IL4 = new JTextField();
		jText_IL4.setText((""));
		appPanel.add(jLabel_IL4);
		appPanel.add(jText_IL4);
		jText_IL4.setBounds(18, 148, 310, 25);

		jLabel_IL5 = new JLabel();
		jLabel_IL5.setText("Selezionare il nome utente");
		jLabel_IL5.setBounds(18, 198, 310, 25);
		jText_IL5 = new JTextField();
		jText_IL5.setText((""));
		appPanel.add(jLabel_IL5);
		appPanel.add(jText_IL5);
		jText_IL5.setBounds(18, 228, 310, 25);

		jLabel_IL6 = new JLabel();
		jLabel_IL6.setText("Selezionare la password");
		jLabel_IL6.setBounds(18, 278, 310, 25);
		jText_IL6 = new JPasswordField(10);
		jText_IL6.setText((""));
		appPanel.add(jLabel_IL6);
		appPanel.add(jText_IL6);
		jText_IL6.setBounds(18, 318, 310, 25);

		jLabel_IL7 = new JButton();
		jLabel_IL7.setText("Controlla connessione");
		jLabel_IL7.setBounds(18, 398, 310, 25);
		appPanel.add(jLabel_IL7);
		jLabel_IL7.setActionCommand("check");
		jLabel_IL7.addActionListener(this);

		jLabel_IL8 = new JLabel();
		jLabel_IL8.setText("");
		jLabel_IL8.setBounds(18, 428, 310, 25);
		appPanel.add(jLabel_IL8);

		optionsPanel.add(appPanel);
		appPanel.setPreferredSize(new java.awt.Dimension(344, 502));
		appPanel.setLayout(null);
		add(optionsPanel, BorderLayout.NORTH);
		optionsPanel.setPreferredSize(new java.awt.Dimension(344, 531));
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(2, 2));
		saveButton = new JButton("SALVA IMPOSTAZIONI");
		saveButton.setActionCommand("save");
		saveButton.addActionListener(this);
		defaultsButton = new JButton("DEFAULT");
		defaultsButton.setActionCommand("defaults");
		defaultsButton.addActionListener(this);
		buttonPanel.add(saveButton);
		saveButton.setPreferredSize(new java.awt.Dimension(293, 36));
		buttonPanel.add(defaultsButton);
		add(buttonPanel, BorderLayout.SOUTH);

		loadSettings();
	}

	/**
	 * defaults() sets the default's values to be used by different options in
	 * the application
	 * */
	private void defaults() {
		jText_IL1.setText("localhost");
		jText_IL4.setText("test");
		jText_IL5.setText("root");
		jText_IL6.setText("root");
	}

	/**
	 * Jigloo methods for drawing windows and handling events
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (command.equals("check")) {
			db = new MySqlDB();
			db.setConnection("localhost", "test", "root", "root");
			if (db.getConnection())
				jLabel_IL8
				.setText("              Connessione stabilita correttamente");
			else
				jLabel_IL8
				.setText("              Impossibile stabilire una connessione");
		} else if (command.equals("defaults")) {
			defaults();
		} else if (command.equals("save")) {
			saveSettings();
			this.dispose();
		}
	}

	/**
	 * loadSettings() loads all the previous settings held into the file into
	 * forms
	 */
	public void loadSettings() {
		sysProps = System.getProperties();
		userProps = new Properties();
		if (configFile.exists() && configFile.canRead()) {

			try {
				FileInputStream fis = new FileInputStream(configFile);
				userProps.loadFromXML(fis);
				fis.close();
				System.out.println();
				jText_IL1.setText(userProps.getProperty("database"));
				jText_IL4.setText(userProps.getProperty("server"));
				jText_IL5.setText(userProps.getProperty("user"));
				jText_IL6.setText(userProps.getProperty("password"));
			} catch (InvalidPropertiesFormatException e) {
			} catch (Exception e) {
			}
		}
	}

	/**
	 * saveSettings() saves the settings into the configuration file
	 */
	@SuppressWarnings("deprecation")
	public void saveSettings() {
		sysProps = System.getProperties();
		userProps = new Properties();
		try {
			boolean rv = true;
			if (!configFile.exists()) {

				rv = configFile.createNewFile();
				if (!rv) {
					throw new FileNotFoundException(
					"Impossibile creare il file di configurazione!");
				}
			}
			if (!configFile.canWrite()) {
				configFile.setWritable(true);
			}
			userProps.setProperty("database", jText_IL1.getText());
			userProps.setProperty("server", jText_IL4.getText());
			userProps.setProperty("user", jText_IL5.getText());
			userProps.setProperty("password", jText_IL6.getText());
			FileOutputStream fos = new FileOutputStream(configFile);
			userProps.storeToXML(fos, "File di impostazione. Non modificare!");
		} catch (Exception e) {
		}
	}

	/**
	 * Returns sistem properties
	 * 
	 * @return
	 */
	public Properties getSysProperty() {
		return this.sysProps;
	}

	/**
	 * windowActivated reads a property that was loaded before and returns its
	 * value
	 * 
	 * @param e
	 *            the e
	 * @return Mixed application properties
	 */

	public void windowActivated(WindowEvent e) {

	}

	public void windowClosed(WindowEvent e) {

	}

	public void windowDeactivated(WindowEvent e) {

	}

	public void windowDeiconified(WindowEvent e) {

	}

	public void windowIconified(WindowEvent e) {

	}

	public void windowOpened(WindowEvent e) {

	}
}